package com.tg.base.concurrent.cpu100;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class Cpu100Test {

    public static void main(String[] args) throws InterruptedException {

//        FutureTask<Int> f1 = build("f1");
//        FutureTask f2 = build("f2");
        Random random = new Random();

        Map<Integer, List<Double>> map = new HashMap<>();
        for (int i = 0; i < 1000; i++) {
            List<Double> list = new ArrayList<>();
            for (int y = 0; y < 5000; y++) {
                list.add(random.nextDouble());
            }
            map.put(i, list);
        }

        System.out.println("begin");
        Executor executorService = Executors.newFixedThreadPool(1000);

        for (int i = 0; i < 1000; i++) {
            final int target = i;
            final List<Double> list = map.get(target);

            executorService.execute(new FutureTask(() -> {
                for (int d = 0; d < 30; d++) {
                    List<Double> collect = list.stream()
                            .sorted(Comparator.comparing(g -> g))
                            .collect(Collectors.toList());

                    System.out.println(collect.get(0));
                }
                return 0;
            }));
        }

        System.out.println("f3取消");
        TimeUnit.SECONDS.sleep(500);

//        f2.cancel(true);
        System.out.println("f2取消");


//        System.out.println("begin");
//        Integer i=0;
//        while (true){
////            System.out.println("go");
//            i++;
//        }
////        System.out.println(i);
    }


}
